feat: make insert_commented (\o) pipe-chain aware#502
feat: make insert_commented (\o) pipe-chain aware#502guilhermegarcia wants to merge 4 commits intoR-nvim:mainfrom
Conversation
When the cursor is inside a multi-line pipe chain (|>, %>%, %<>%, %T>%, or + for ggplot), \ro now collects the full chain instead of just the current line. The output is inserted as a comment below the last line of the chain. Single-line behavior is unchanged.
- Walk-down now tracks parenthesis depth so multi-line function args (e.g., mutate(\n...\n)) are included in the chain - Walk-up tracks reverse paren depth so cursor on `)` lines finds the matching `(` above - Remove `+` from pipe operators (printing ggplot objects as comments is not useful)
|
Thank you! @PMassicotte has made sending code to R pipe-chain aware, and he may want to review this pull request. I can also look at this (but only tomorrow). |
|
Thank you @guilhermegarcia for this PR. Can you try the feat/insert-commented-reuse-chain branch (986a6df)? I redid it but leveraging the use of the existing infra based on Treesitter. It should work also in quarto and rmd document. |
|
I git-goofed, please use https://github.com/R-nvim/R.nvim/tree/feat/insert-commented-pipe-chain |
|
Thanks @PMassicotte! I tested your branch and it works great — much cleaner than my text-based approach. Using the existing treesitter infrastructure makes more sense, and the Quarto/Rmd support is a nice bonus. Happy to close this PR in favor of your implementation. |
Thank you very much for your issue and proposing a solution :) |

Summary
insert_commented(\o) now detects multi-line pipe chains and sends the full expression instead of just the current line|>,%>%,%<>%, and%T>%operatorsmutate(\n...\n))Motivation
Currently, pressing
\oon any line of a multi-line pipe chain like:only sends that single line (e.g.,
print(c(3, 1, 4, 1, 5) |>)), which is an incomplete expression — R produces no output. Pipe chains are arguably the most common multi-line pattern in modern R, so this is a significant usability gap.Similarly, pipes into multi-line function calls like:
are now correctly captured as a complete expression by tracking
(/)depth.Approach
Uses a text-based line-walking heuristic with parenthesis depth tracking:
)needing a(abovedepth > 0) or while the line ends with a pipe operatorThe collected lines are joined with spaces (not newlines) to avoid issues in the Lua → LSP → rnvimserver → R transport layer.
This avoids the complexity of treesitter (and the synthetic buffer mapping needed for Quarto/Rmd) while covering all practical cases.
Test plan
x <- 5, press\o→# [1] 5below (unchanged behavior)data |> filter() |> summarize()chain → full chain evaluated, output below last linetokens |> mutate(\n...\n)→ complete expression capturedresult <- data |> filter()→ assigns and printsrev() # comment→ comment removed before sending%>%